package org.wm.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.wm.springcloud.service.PaymentHystrixService;

import javax.annotation.Resource;

/**
 * @ClassName:OrderHystrixController
 * @Description: 订单控制器
 * @Author:Deamer
 * @Date:2021/12/17 21:32
 **/
@RestController
@Slf4j
public class OrderHystrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;


    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        return paymentHystrixService.paymentInfo_OK(id);
    }


    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    })
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
        // int a = 10 / 0;
        return paymentHystrixService.paymentInfo_TimeOut(id);
    }

    // 善后方法
    public String paymentTimeOutFallbackMethod(Integer id) {
        return "我是消费者80，对方支付方繁忙，请稍后再试或者自身运行出错，请检查！id:" + id;
    }
}
